package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

import com.flaviofaria.kenburnsview.KenBurnsView;
import com.flaviofaria.kenburnsview.RandomTransitionGenerator;
import com.github.postsanf.yinian.bean.YNNativeImage;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/12/19.
 */

public class ViewPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<YNNativeImage> mImageAlbums;

    public ViewPagerAdapter(Context context, List<YNNativeImage> mImageAlbums) {
        this.mContext = context;
        this.mImageAlbums = mImageAlbums;
    }

    @Override
    public int getCount() {
        return mImageAlbums.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        View view= LayoutInflater.from(mContext).inflate(R.layout.yn_item_machine,null);
//        ImageView iv=(ImageView) view.findViewById(R.id.iv_item_machine_bg);
//        ImageView iv = new ImageView(mContext);
        String url = mImageAlbums.get(position).getPath();
        final KenBurnsView iv_image_browser = new KenBurnsView(mContext);
        iv_image_browser.setTransitionGenerator(new RandomTransitionGenerator(5000, new LinearInterpolator()));
        ImageLoader.getInstance().displayImage(url, iv_image_browser);
        final boolean bb = false;
        iv_image_browser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!bb) {
                    iv_image_browser.pause();
                }else{
                    iv_image_browser.resume();
                }
            }
        });
        try {
            container.addView(iv_image_browser, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iv_image_browser;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }
}